package com.leyou.common.advice;

import com.leyou.common.exception.ExceptionResult;
import com.leyou.common.exception.LyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//service模块中的启动类会扫描到这个类，
// 所以本模块不需要配置启动类，也可使得此异常拦截器生效
//统一异常拦截器
@ControllerAdvice  //该注解会拦截所有加了 @controller的类
@Slf4j
public class BasicExceptionAdvice {

    @ExceptionHandler(LyException.class)  //截取自定义异常
    //ResponseEntity<String> 可以处理http响应
    public ResponseEntity<ExceptionResult> handleLyException(LyException e){
        return ResponseEntity.status(e.getStatus()).body(new ExceptionResult(e));
    }
//
//    @ExceptionHandler(RuntimeException.class)  //截取运行时异常
//    public ResponseEntity<String> handleRuntimeException(RuntimeException e){
//        return ResponseEntity.status(500).body(e.getMessage());
//    }
}
